<?php
 function RandNumber($e){
	 for($i=0;$i<$e;$i++){
		$rand =  $rand .  rand(0, 9);  
	 }
	 return $rand;
 }
	if($_GET['do']== 1){
		$pdo = new PDO('mysql:host=localhost;dbname=uwika_krs_alpha', 'root', '');
		$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
		$sql = "SELECT nim_dosen FROM dosen";
		$stmt = $pdo->prepare($sql);
		$stmt->execute();
		$sqlinsert = "INSERT INTO security (username, password, level) values (:username, :password, '2') ";
		while($_data = $stmt->fetch(PDO::FETCH_OBJ)){
			$sql2 = "SELECT username FROM security WHERE username= :username";
			$stmt2 = $pdo->prepare($sql2);
			$stmt2->execute(array(':username' => $_data->nim_dosen ));
			if( $stmt2->rowCount() <= 0 ){
				$password = RandNumber(8);
				echo $password;
				echo '<Br />';
				$stmtinsert = $pdo->prepare($sqlinsert);
				$stmtinsert->execute(array(':username' => $_data->nim_dosen ,':password' => $password 
				)) or die("ERROR: " . implode(":", $stmtinsert->errorInfo()));
			}		
		}
		echo 'data affected : '.$count.' from '.$countall;
	}

	/*
	$sql = "INSERT INTO dosen (nim_dosen, nip, nama) values (:nim_dosen, :nip, :nama ) ";
	
	foreach($_data as $_data_){
		$depan = substr($_data_->NIP,2,3).substr($_data_->NIP,-3,3);
		echo $depan;
		$sql2 = "SELECT nim_dosen FROM dosen";
		$stmt2 = $pdo->prepare($sql2);
		$stmt2->execute();
		$isi = $stmt2->rowCount()+1;
		if( $isi < 10 ){ $isi = '00'.$isi; }
		elseif( $isi < 100 ){ $isi = '0'.$isi;}
		echo $isi;
		var_dump($_data_);echo '<br \>';
		
		if( !empty($_data_->NAMA)) {
			$stmt = $pdo->prepare($sql);
			$stmt->execute(array(':nim_dosen' => $depan.$isi ,':nama' => $_data_->NAMA 
								,':nip' => $_data_->NIP
			)) or die("ERROR: " . implode(":", $stmt->errorInfo()));
		}

		}
	*/


?>